import { apiInit } from '@/api/app'

export const state = () => ({
  alarmData: [],
  newAlarms: [],
  enableVoice: false,
  enableMock: false
})

export const mutations = {
  /**
   * 缓存异常预警数据
   */
  cacheAlarmDataList(state, data) {
    state.alarmData.push(...data)
  },
  /**
   * 缓存用于动态展示的异常预警数据
   */
  cacheAlarmData(state, data) {
    let cacheData = state.alarmData
    cacheData.unshift(data)
    state.alarmData = cacheData

    // 缓存最新接受的异常预警数据
    state.newAlarms.push(data.id)
  },
  /**
   * 清除新增异常预警数据
   */
  clearNewAlarm(state) {
    state.newAlarms.length = 0
  },
  logout(state) {
    state.enableVoice = false
    state.enableMock = false
  }
}

export const actions = {
  /**
   * 获取异常预警数据
   * @param {Array} params 查询参数
   */
  async fetchInitAlarmDatas({ commit, state }, params) {
    return await this.$axios.get(apiInit()).then(data => {
      // 获取初始化异常预警数据后缓存
      let initData = data.data.filter(item => {
        return item.status === 0
      })
      commit('cacheAlarmDataList', initData)
      return initData
    })
  }
}
